<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "post".
 *
 * @property integer $id
 * @property string $title
 * @property string $url
 * @property string $content
 * @property integer $sign
 * @property integer $type
 * @property string $thumb
 * @property integer $zan
 * @property integer $cai
 * @property string $create_at
 * @property integer $user_id
 * @property integer $cate_id
 * @property integer $status
 * @property integer $view
 * @property string $ip_address
 * @property string $keywords
 * @property string $description
 * @property string $excerpt
 * @property string $signature
 * @property string $nickname
 * @property string $img
 * @property integer $last_comment
 *
 * @property Comment[] $comments
 * @property User $user
 * @property Category $cate
 * @property Report[] $reports
 * @property UserPost[] $userPosts
 * @property User[] $users
 * @property ZancaiPost[] $zancaiPosts
 */
class Post extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'post';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'content', 'type', 'cate_id'], 'required'],
            [['content'], 'string'],
            [['sign', 'type', 'zan', 'cai', 'user_id', 'cate_id', 'status', 'view', 'last_comment'], 'integer'],
            [['title', 'keywords'], 'string', 'max' => 100],
            [['url', 'thumb', 'description', 'excerpt', 'nickname', 'img'], 'string', 'max' => 255],
            [['create_at'], 'string', 'max' => 20],
            [['ip_address'], 'string', 'max' => 50],
            [['signature'], 'string', 'max' => 60],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
            [['cate_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['cate_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'url' => 'Url',
            'content' => 'Content',
            'sign' => 'Sign',
            'type' => 'Type',
            'thumb' => 'Thumb',
            'zan' => 'Zan',
            'cai' => 'Cai',
            'create_at' => 'Create At',
            'user_id' => 'User ID',
            'cate_id' => 'Cate ID',
            'status' => 'Status',
            'view' => 'View',
            'ip_address' => 'Ip Address',
            'keywords' => 'Keywords',
            'description' => 'Description',
            'excerpt' => 'Excerpt',
            'signature' => 'Signature',
            'nickname' => 'Nickname',
            'img' => 'Img',
            'last_comment' => 'Last Comment',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getComments()
    {
        return $this->hasMany(Comment::className(), ['post_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCate()
    {
        return $this->hasOne(Category::className(), ['id' => 'cate_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getReports()
    {
        return $this->hasMany(Report::className(), ['post_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUserPosts()
    {
        return $this->hasMany(UserPost::className(), ['post_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUsers()
    {
        return $this->hasMany(User::className(), ['id' => 'user_id'])->viaTable('user_post', ['post_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getZancaiPosts()
    {
        return $this->hasMany(ZancaiPost::className(), ['post_id' => 'id']);
    }
}
